1. Create a new Console Application project and write a program to print a number to console as a word. This program is to replace for you a traditional ‘Hello World’ example. It is required to accept an integer value as input (i.e. numbers 1 to 9), and then print the corresponding word on screen (i.e. "ONE", "TWO", ... , "NINE”). If anything other than 1 to 9 is entered, then an error message is displayed. This simple program is used to make you thinking about validation; that is, has the user input only integer values between 1 and 9? If not, then we need to output an error message. Give a new name to the main class by renaming the default Program class name (and the associated file name) to IfStatement in the Solution Explorer of your IDE (Microsoft Visual Studio or Studio Code). Type the following code into the main IfStatement class and complete the other else if statements. Complete the missing parts of the program for the remaining numbers 2 to 9. Compile and run the program, then examine the produced output. You should aim to test your program with the following: All nine integers between 1 and 9. A few other integers outside this range, i.e. 10 and 100. What happens if you enter ‐5? What happens if you enter a letter, e.g. ‘a’? How can you add an error message to catch this? (HINT: We normally use a try‐catch block to catch the error. Do not worry if you are unable to complete this now, we will look at it in more detail later on in the unit.) Ans. using System; namespace IfStatementApp{ class IfStatement{ static void Main(String[] args){ Console.WriteLine("Enter the number (as an integer):"); try{ int Number = Convert.ToInt32(Console.ReadLine()); if (Number == 1) { Console.WriteLine("One"); } else if (Number==2) { Console.WriteLine("Two"); } else if (Number==3) { Console.WriteLine("Three"); } else if (Number==4) { Console.WriteLine("Four"); } else if (Number==5) { Console.WriteLine("Five"); } else if (Number==6) { Console.WriteLine("Six"); } else if (Number==7) { Console.WriteLine("Seven"); } else if (Number==8) { Console.WriteLine("Eight"); } else if (Number==9) { Console.WriteLine("Nine"); } else { Console.WriteLine("Invalid Input. Please enter an integer between 1 and 9"); } } catch(Exception e){ Console.WriteLine(e.Message); } } } } 2. Now, create a new project and name its main class (and file) as SwitchStatement. Implement another version of the above program by writing the following code. Complete the rest of the case statements up to 9 (between case 2 and default). Run the program and check whether its output is as expected. Test it with the following: All nine integers between 1 and 9. A few other integers outside this range, i.e. 10 and 100. What happens if you enter ‐5? What happens if you enter a letter, e.g. ‘a’? How can you add an error message to catch this? Ans. using System; using System.IO; class SwitchStatement { static void Main(String[] args) { Console.WriteLine("Enter the number (as an integer):"); try{ int number = Convert.ToInt32(Console.ReadLine()); switch(number) { case 1: Console.WriteLine("One");break; case 2: Console.WriteLine("Two");break; case 3: Console.WriteLine("Three");break; case 4: Console.WriteLine("Four");break; case 5: Console.WriteLine("Five");break; case 6: Console.WriteLine("Six");break; case 7: Console.WriteLine("Seven");break; case 8: Console.WriteLine("Eight");break; case 9: Console.WriteLine("Nine");break; default: Console.WriteLine("Invalid Input. Enter an integer between 1-9");break; } } catch(Exception e) { Console.WriteLine(e.Message); } } } 3.Find all existing issues with the code snippets presented below. It is highly important to try working this out on paper by tracing through the statements and then input the code into your IDE. Run it to see if you are correct, and then see if you can fix the code. int number = 50; if (number == 50) ; { Console.WriteLine(“Number is 50”); } int number = 60; if (number >== 50 and number <== 100) { Console.WriteLine(“Number is more than or equal to 50 and less than or equal to 100”); } public class Score { public static void main(String[] args) { double score = 40; if score > 40{ Console.WriteLine("You passed the exam!"); } else score < 40{ Console.WriteLine("You failed the exam!"); } } } Switch (n) { case 1: Console.WriteLine(“The number is 1”); case 2: Console.WriteLine (“The number is 2”); break; default: Console.WriteLine (“The number is not 1 or 2”); break; } switch (n) { Case 1: Console.WriteLine (“A”); break; case2: Console.WriteLine (“B”); break; Default: Console.WriteLine (“C”); break; } Ans. //Code Snippets int number = 50; if (number == 50){ //Removed ; after the if condition Console.WriteLine("Number is 50"); } //************************************************************************ int number = 60; if (number >=50 || number<=100){ //Changed relational operators and and to || Console.WriteLine("Number is more than or equal to 50 or less than or equal to 100"); } //******************************************* public class Score{ public static void Main(String[] args){ //changes main to Main double score = 40; if (score>=40){ //enclosed condition in () Console.WriteLine("You passed the exam!"); } else if(score<40){ // use else if for condition; although the condition is somewhat redundant here for valid arguments Console.WriteLine("You failed the exam!"); } } } //******************************************************************** switch(n){ //Switch - switch case 1: Console.WriteLine("The number is 1");break; //break added case 2: Console.WriteLine("The number is 2");break; default: Console.WriteLine("The number is not 1 or 2");break; } //******************************************************************* switch(n){ case 1: Console.WriteLine("A"); break; //Case is changed to case case 2: Console.WriteLine("B"); break; //space between case and 2 default: Console.WriteLine("C"); break; //Changed Default to default } 4. What is the output of the following code fragments? Remember to try working this out on paper by tracing through the statements and then input the code into. Run it to see if you are correct, and then see if you can fix the code. int height = 13; if ( height <= 12 ) Console.WriteLine("Low bridge: "); Console.WriteLine ("proceed with caution."); int sum = 21; if ( sum != 20 ) Console.WriteLine ("You win "); else Console.WriteLine ("You lose "); Console.WriteLine ("the prize."); int sum = 7; if ( sum > 20 ) { Console.WriteLine ("You win "); } else { Console.WriteLine ("You lose "); } Console.WriteLine ("the prize."); Ans. //Corrected code snippets //*************************************************** int height = 13; if (height<=12){ Console.WriteLine("Low Bridge: "); Console.WriteLine("proceed with caution.") } /* Output: Nothing since height is not less than or equal to 13 */ //************************************************** int sum = 20; if (sum!=20){ Console.WriteLine("You win "); } else{ Console.WriteLine("You lose "); } Console.WriteLine("the prize"); /* Output: You lose the prize */ //****************************************** int sum = 7 if (sum>20){ Console.WriteLine("You win "); }else{ Console.WriteLine("You lose "); } Console.WriteLine("the prize"); 5. A microwave oven manufacturer recommends that when heating two items you should add 50% to the heating time, and when heating three items you should double the heating time. Heating more than three items at once is not recommended. Create a new project and name its main class (and file) as Microwave. Write a program that asks the user for the number of items and the single‐item heating time. Perform the calculation, and print out the recommended heating time Ans. using System; using System.io; class Microwave { static void Main(String[] args) { Console.WriteLine("Enter the number of items you want to heat in the owen:"); int item = Convert.ToInt32(Console.ReadLine()); Console.WriteLine("\nEnter the single item heating time in minutes:"); double time = Convert.ToDouble(Console.ReadLine()); double total = 0; int result = 1; if(item==1) { total = time; } else if(item==2) { total = time*1.5; } else if(item==3) { total = time*2.0; } else if(item>3) { Console.WriteLine("\nThe company doesn't recommend heating more than three items at a time"); result = 0; } else { Console.WriteLine("\nInvalid Number of items"); result = 0; } if(result==1) { Console.WriteLine("Set the time to "+total+" minutes"); } } } 6. This exercise requires you to cast the result of an operation performed on two integers to a double. You should have already used casting to convert a String to an int. via Convert.ToInt32(Console.ReadLine()); as a combination of the two commands. The compiler will not let you assign a value to a variable if it is the wrong type – even if that variable can hold the value. When you use casting, you are essentially making a promise to the compiler that you know the types are different and that in this particular instance it is alright for C# to push data into a new variable. Create a new project and name its main class (and file) as DoCasting. Write a program by following the steps below: Declare and initialise variables for two integer values sum and count. Initialise these variables to 17 and 5 respectively. Declare an integer variable called intAverage. Calculate the integer average, intAverage, using sum/count. Print out the integer average, intAverage – is this correct? Declare a double variable called doubleAverage. Now try calculating the double average, doubleAverage, using sum/count – what is the result? Is this correct? Cast the sum variable to a double using (double)sum and divide this by count, and print the result – is this correct? Remember that the process for casting is to place the data type in parentheses before the operation that is being performed, which in this case is sum/count. Ans using System; using System.IO; class DoCasting{ static void Main(String[] args){ int sum = 17; int count = 5; int intAverage = sum/count; Console.WriteLine("Integer Average :"+intAverage); double doubleAverage = sum/count; Console.WriteLine("Double Average :"+doubleAverage); Console.WriteLine("Correct Average :"+((double)sum/count)); } } 7. Design a algorithm,first on paper,using pseudocode which prints the sum of 1,2,3,...,to an upper bound determined by user input(e.g. 100). Then create a new C# Console Application project and write a program to achieve this. Once coded, it should also compute and display the average of the numbers. The output should look like:  The sum is 5050  The average is 50.5 Give a new name to the main class of the project by changing it from the default Program to Repetition in the Solution Explorer of yourIDE (Microsoft Visual Studio or Studio Code). Type the following code into the Main method of the Repetition class. Complete the missing parts, compile and run the program.Check whetherthe produced output is as expected. Ans using System; using System.io; class Repetition{ static void Main(String args[]){ Console.WriteLine("Enter the upperbound:"); int n = Convert.ToInt32(Console.ReadLine()); double sum = 0; double average; for(int i=1;i<=n;i++){ sum = sum+i; } Console.WriteLine("Sum of series = "+sum); average = double(sum)/n; Console.WriteLine("Average of series = "+average); } } 8. Add a new block of code into the Main method. Modify the code written in the first task to use a while loop instead ofthe for loop.What is the difference between for and while loops? If you want to see what is happening in the loops then trace sum and number. Ans using System; using System.io; class Repetition{ static void Main(String args[]){ Console.WriteLine("Enter the upperbound:"); int n = Convert.ToInt32(Console.ReadLine()); double sum = 0; double average; int i = 1; while(i<=n) { sum = sum+i; i++; } Console.WriteLine("Sum of series = "+sum); average = sum/n; Console.WriteLine("Average of series = "+average); } } //The difference between for and while loops is that all the elements of for loop are at one place and the elements of while loop are scattered 9.Add one more block of code into the Main method. Modify the code to use a do … while loop instead of a while loop. Replace the while loop with the following code. Check that the output from all three loops is the same. What is the difference between for, while and do … while loops? Ans. using System; using System.io; class Repetition{ static void Main(String args[]){ Console.WriteLine("Enter the upperbound:"); int n = Convert.ToInt32(Console.ReadLine()); double sum = 0; double average; int i = 1; do { sum = sum+i; i++; }while(i<=n); Console.WriteLine("Sum of series = "+sum); average = sum/n; Console.WriteLine("Average of series = "+average); } } //The difference between do-while and while/for loops is that do while loop is exit controlled //this means that the condition in do while loop is checked after execution of loop body //In contrast for and while loops are entry controlled 10.Find existing issues for all the code snippets presented below. It is highly important to try working this out on paper by tracing through the statements and then inputthe code into yourIDE. Run itto see if you are correct, and then see if you can fix the code. You may wish to add additional Console.WriteLine statements to check that variables contain the values you expect. Note thatthere may be more than one mistake. int c = 0, product = 0; while (c <= 5) product = product * 5; c = c + 1; int a = 31, b = 0, sum = 0; while (a != b) { sum = sum + a; b = b + 2; } int x = 1; int total; while (x <= 10) { total = total + x; x = x + 1; } while (y < 10) { int y =0; Console.WriteLine(“y” + y); y = y + 1; } int z = 0; while (z > 0) { z = z ‐ 1; Console.WriteLine("z: " +z); } for(int count = 1, count < 100, count++) { Console.WriteLine("Hello"); } for(int I =1; i>10; i++) { if (i>2) { Console.WriteLine (“Flower”); } } ans. //************************************* int c=1,prod=1; while(c<=5){ product = product*c; //Factorial of 5 c=c+1; } Console.WriteLine(product); //*************************************** int a = 31,b=0,sum=0; while(a>=b){ sum = sum+b; //Program is adding even number upto 30 b = b+2; } Console.WriteLine(sum); //*************************************** int x=1; int total = 0; while(x<=10){ total = total+x; x=x+1; } Console.WriteLine(x); //**************************************** int y=0; while(y<10){ Console.WriteLine("y:",y); y=y+1; } //***************************************** int z = 10; while(z>0){ z=z-1; Console.WriteLine("z:",z); } //********************************************* for(int count=1;count<100;count++){ Console.WriteLine("Hello"); } //******************************************** for(int i=1;i<10;i++){ if(i>2){ Console.WriteLine("Flower"); } } 11.Rewrite both ofthe following fragments of code using for loops instead of while loops. int sum = 0 ; int j = ‐5 ; while ( sum <= 350 ) { sum += j ; j += 5 ; } int x = 0; while ( x < 500 ) { Console.WriteLine( x ); x = x + 5; } ans //********************************************************** int sum = 0; int j; for(int j=-5;sum<=350;j=j+5){ sum = sum+j; } //************************************************************ for(int x=0;x<500;x=x+5){ Console.WriteLine(x); } 12. Write a program that will continue to prompt the user to guess a number until the correct number has been entered. You should also consider user validation; that is, has the user entered a number less than 1 or greater than 10? Have they entered a character? Start by creating a new project and name its main class (and file) as GuessingNumber. Then proceed with the following steps: Create an integer variable called number and setit equalto 5. Prompt the user to guess a number between 1 and 10. Ifthe user enters number 5, print “You have guessed the number! Well done!” If they do not enter 5, continue to promptthe user to enter a number until they enter Ans. using System; using System.IO; class GuessingNumber{ static void Main(String[] args){ int number = 5; int result = 0; while(true){ Console.WriteLine("\nEnter a guess:"); int guess = Convert.ToInt32(Console.ReadLine()); if(number == guess){ Console.WriteLine("\nYou have guessed the number! Well done!"); result = 1; break; } else if(guess<0 || guess>10){ Console.WriteLine("\nPlease keep you guess in range"); } else{ Console.WriteLine("\nYour guess is wrong but it is in range"); } } if(result == 1){ Console.WriteLine("Thanks for playing!"); } } } 13.. Compile, run and check the program for potential errors. Now, alterthe program so thatittakes a number entered by a user(user 1). This numberis set as the number to be guessed by user 2. Then ask (user 2)to input their own number. Check to see if the number user 2 entered is equal to the number user 1 entered. Continue this process until the user 2 guesses the correct number. Remember that for this program you should validate the user’s input. Finally, run and check the program again. Ans. using System; using System.IO; class GuessingNumber{ static void Main(String[] args){ Console.WriteLine("User1, please enter a number:"); int number = Convert.ToInt32(Console.ReadLine()); int result = 0; while(true){ Console.WriteLine("\nEnter a guess, User2 :"); int guess = Convert.ToInt32(Console.ReadLine()); if(number == guess){ Console.WriteLine("\nYou have guessed the number! Well done!"); result = 1; break; } else if(guess<0 || guess>10){ Console.WriteLine("\nPlease keep you guess in range"); } else{ Console.WriteLine("\nYour guess is wrong but it is in range"); } } if(result == 1){ Console.WriteLine("Thanks for playing!"); } } } 14. Write a program which prints all the numbers between 1 and n, where n is provided by the user, that are evenly divisible by four but not by five. You should use a for loop forthis question. Create a new project and name its main class (and file) as DivisibleFour. Compile and run the program. Check whether the produced output is as expected. Ans. using System; using System.IO; class DivisibleFour{ static void Main(String[] args){ Console.WriteLine("Enter the value of n:"); int n = Convert.ToInt32(Console.ReadLine()); Console.Write("\nPrinting Multiples of 4 that are not divisible by 5:\n"); for(int i=1;i<=n;i++){ if(i%4==0 && i%5!=0){ Console.WriteLine(i+" "); } } } }